package pixelwaks.ctrl.impl;

import java.util.ArrayList;
import java.util.List;

import common.ctrl.IObserver;
import common.gui.IViewable;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;

public class PlayerCtrl implements IObserver {

	private static Game game = Game.getInstance();
	private List<IViewable> observers = new ArrayList<IViewable>();
	public final static int MAX_CHARGE_STRENGTH = 150;
	
	public void aimLeft() {

		double tmp = game.getCurrentPlayer().getRadians() - Math.PI / 180;

		if (-(Math.PI * 7) / 18 < tmp) {
			game.getCurrentPlayer().setRadians(tmp);
			updateView();
		}
	}

	public void aimRight() {

		double tmp = game.getCurrentPlayer().getRadians() + Math.PI / 180;

		if (Math.PI / (9.0 / 8.0) - Math.PI / 2 > tmp) {
			game.getCurrentPlayer().setRadians(tmp);
			updateView();
		}
	}

	public boolean changeChargeStrength() {
		
		int cs = game.getCurrentPlayer().getChargeStrength();
		boolean ok = false;
		
		if(game.getCurrentPlayer().getCurrentWeapon().getNumberOfShots() != 0) {		
			
			if(cs < MAX_CHARGE_STRENGTH) {
				game.getCurrentPlayer().setChargeStrength(cs+2);
				ok = true;
			} else {
				ok = false;
			}
			
			if(cs != 0)
				updateView();			
			
		}
		return ok;
	}
	
	public void resetChargeStrength() {
		
		game.getCurrentPlayer().setChargeStrength(0);
	}
	
	public void setStatus(Player player, double status) {

		player.setStatus(status);
		updateView();
	}

	@Override
	public void addView(IViewable iv) {
		
		observers.add(iv);
	}	
	
	private void updateView() {
		
		for (IViewable view : observers)
			view.updateView(null);
	}
}
